package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.untils.UiNameConst;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;

	/**
	 * 左右按钮翻页组件，不显示总共有多少页的（样式如：<  >）
	 * 主要参数
	 * <li>min 最小值</li>
	 * <li>value 当前值</li>
	 * <li>index 当前相对值</li>
	 * <li>max 最大值</li>
	 * value in [min,max] (MAX <= MIN + midStr.length-1)
	 * @author liangqian
	 */
	public class PageTurning extends Component
	{
		public static const PAGETURNING_STYLE_1:int = 10;
		public static const PAGETURNING_STYLE_2:int = 20;
		private static const FILTERS:DropShadowFilter = new DropShadowFilter(3,90,0,1,3,3,.3);
		private var pageIconWidth:int = 38;
		/**
		 * 根据list和最小值 自动计算最大值
		 */
		private var _min:int = 0;
		/**
		 * 当前页数
		 */
		private var _value:int = _min;
		/**
		 * 总页数
		 */
		private var _max:int = _min;

		/**
		 * 下一页的回调函数
		 */
		private var onChange:Function;

		private var _spacing:int;
		public var isClickBtn:Boolean;
		/**
		 *当前翻的是否是下一页 
		 */		
		public var _isNext:Boolean;
		private var _style:int;

		/**
		 * 构造函数
		 * @param parent 父级显示容器
		 * @param xpos
		 * @param ypos
		 * @param spacingN
		 * @param handler  onChange();
		 * 
		 */		
		public function PageTurning(parent:DisplayObjectContainer = null , xpos:int = 0 , ypos:int = 0 , spacingN:int = 5, handler:Function = null , style:int = 10)
		{
			_style = style;
			super(parent , xpos , ypos);
			onChange = handler;
			spacing = spacingN;
		}

		private var preMc:MovieClip;
		private var nextMc:MovieClip;
		/**
		 * Overriden in subclasses to create child display objects.
		 */
		override protected function addChildren():void
		{
			if(_style == PAGETURNING_STYLE_1)
			{
				preMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON, 'leftPage') as Class) as MovieClip; 
				nextMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON, 'rightPage') as Class) as MovieClip;
				
			}
			else
			{
				preMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.ACTIVITY_VIEW, 'leftPageWithGift') as Class) as MovieClip; 
				nextMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.ACTIVITY_VIEW, 'rightPageWithGift') as Class) as MovieClip;
			}
			pageIconWidth = preMc.width;
			preMc.gotoAndStop(4);
			nextMc.gotoAndStop(4);
			preMc.filters = nextMc.filters = [FILTERS];
			addChild(preMc);
			addChild(nextMc);
			max = 0;
		}

		/**
		 * 强制校正当前值
		 */
		protected function update():void
		{
			_value =  Math.min(_max , Math.max(_min , _value));
			if(_value > _min)
			{
				if(!preMc.buttonMode)
				{
					preMc.buttonMode = preMc.enabled = true;
					preMc.gotoAndStop(1);
					preMc.addEventListener(MouseEvent.ROLL_OVER,preHoverHandler);
					preMc.addEventListener(MouseEvent.CLICK,prePageHandler);
				}
			}
			else
			{
				if(preMc.buttonMode)
				{
					preMc.buttonMode = preMc.enabled = false;
					preMc.gotoAndStop(4);
					preMc.removeEventListener(MouseEvent.ROLL_OVER,preHoverHandler);
					preMc.removeEventListener(MouseEvent.CLICK,prePageHandler);
					preMc.removeEventListener(MouseEvent.ROLL_OUT,preOutHandler);
				}
			}
			
			if( _value != _max)
			{
				if(!nextMc.buttonMode)
				{
					nextMc.buttonMode = nextMc.enabled = true;
					nextMc.gotoAndPlay(1);
					nextMc.addEventListener(MouseEvent.ROLL_OVER,nextHoverHandler);
					nextMc.addEventListener(MouseEvent.CLICK,nextPageHandler);
				}
			}
			else
			{
				if(nextMc.buttonMode)
				{
					nextMc.buttonMode = nextMc.enabled = false;
					nextMc.gotoAndStop(4);
					nextMc.removeEventListener(MouseEvent.ROLL_OVER,nextHoverHandler);
					nextMc.removeEventListener(MouseEvent.CLICK,nextPageHandler);
					nextMc.removeEventListener(MouseEvent.ROLL_OUT,nextOutHandler);
				}
			}

			invalidate();
		}
		
		public function scaleXY(n:Number):void
		{
			preMc.scaleX = preMc.scaleY = nextMc.scaleX = nextMc.scaleY = n;
		}

		/**
		 * 设置最小值和当前值,最大值由最小值+可选list的长度决定
		 * @param mi 最小值
		 * @param ma 最大值
		 * @param val 当前
		 */
		public function setMinMaxValue(mi:int , ma:int , val:int):void
		{
			_min = mi;
			_max = Math.max(ma,mi);
			value = val;
		}

		override public function setSize(w:int , h:int):void
		{
		}

		override public function draw():void
		{
			_width = pageIconWidth * 2 + _spacing;
			nextMc.x = _width - pageIconWidth;
//			super.x = _x - width * .5;
		}

		/**
		 * 下一页
		 */
		private function nextPageHandler(e:MouseEvent):void
		{
//			nextMc.gotoAndStop(3);
			isClickBtn = true;
			_isNext = true;
			value++;
			if (onChange is Function)
				onChange(this);
		}

		/**
		 * 上一页
		 */
		private function prePageHandler(e:MouseEvent):void
		{
//			preMc.gotoAndStop(3);
			isClickBtn = true;
			_isNext = false;
			value--;
			if (onChange is Function)
				onChange(this);
		}

		private function preHoverHandler(e:MouseEvent):void
		{
			preMc.gotoAndStop(2);
			preMc.addEventListener(MouseEvent.ROLL_OUT,preOutHandler);
		}
		
		private function nextHoverHandler(e:MouseEvent):void
		{
			nextMc.gotoAndStop(2);
			nextMc.addEventListener(MouseEvent.ROLL_OUT,nextOutHandler);
		}
		
		private function preOutHandler(e:MouseEvent):void
		{
			preMc.gotoAndStop(1);
			preMc.removeEventListener(MouseEvent.ROLL_OUT,preOutHandler);
		}
		
		private function nextOutHandler(e:MouseEvent):void
		{
			nextMc.gotoAndStop(1);
			nextMc.removeEventListener(MouseEvent.ROLL_OUT,nextOutHandler);
		}
		
		public function get spacing():int
		{
			return _spacing;
		}

		public function set spacing(value:int):void
		{
			_spacing = value;
//			invalidate();
			draw();
		}

		private var _offset:int = 1;
		public function get index():int
		{
			return _value + _offset;
		}

		/**
		 * 返回当前页数 ∈[_min,_max]
		 */
		public function get value():int
		{
			return _value;
		}

		public function set value(num:int):void
		{
			_value = num;
			update();
		}

		public function get max():int
		{
			return _max;
		}

		public function set max(num:int):void
		{
			setMinMaxValue(_min , num , _value);
		}

		public function get min():int
		{
			return _min;
		}

		public function set min(num:int):void
		{
			setMinMaxValue(num , _max , _value);
		}
	}
}